package com.systemsjr.mpf.account;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.systemsjr.mpf.IntentHandler;
import com.systemsjr.mpf.MpfActivity;
import com.systemsjr.mpf.R;

public class ManageAccountActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manage_account);

		final EditText codeEditText = (EditText) findViewById(R.id.codeEditText);
		final EditText accountEditText = (EditText) findViewById(R.id.accountEditText);
		final Account account = new Account();

		Bundle bundle = getIntent().getExtras();

		if (bundle != null && bundle.size() > 0) {
			if (bundle.getLong(Account.ID) > 0) {
				account.setId(bundle.getLong(Account.ID));
			}

			if (bundle.getString(Account.CODE) != null) {
				account.setCode(bundle.getString(Account.CODE));
			}

			if (bundle.getString(Account.ACCOUNT) != null) {
				account.setAccount(bundle.getString(Account.ACCOUNT));
			}
		}

		codeEditText.setText(account.getCode());
		accountEditText.setText(account.getAccount());

		Button saveButton = (Button) findViewById(R.id.saveAccountButton);
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				account.setCode(codeEditText.getText().toString());
				account.setAccount(accountEditText.getText().toString());

				ContentResolver cr = getContentResolver();
				ContentValues values = new ContentValues();
				values.put(Account.CODE, account.getCode());
				values.put(Account.ACCOUNT, account.getAccount());

				if (account.getId() == null || account.getId() == 0) {
					cr.insert(Account.CONTENT_URI, values);

					String s = Account.CODE + " = '" + account.getCode() + "'";
					Cursor c = cr.query(AccountInstance.CONTENT_URI,
							AccountInstance.PROJECTIONS, s, null, null);

					if (c.moveToFirst()) {
						account.setId(c.getLong(c.getColumnIndex(Account.ID)));
					}
					c.close();

				} else {
					cr.update(Account.CONTENT_URI, values,
							"_id = " + account.getId(), null);
				}

				showList("", "");
			}
		});

		Button newButton = (Button) findViewById(R.id.newAccountButton);
		newButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				codeEditText.setText(null);
				accountEditText.setText(null);
				account.setCode(codeEditText.getText().toString());
				account.setAccount(accountEditText.getText().toString());
			}
		});

		Button deleteButton = (Button) findViewById(R.id.deleteAccountButton);
		deleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentResolver cr = getContentResolver();
				if (account.getId() != null || account.getId() > 0) {
					cr.delete(Account.CONTENT_URI, "_id = " + account.getId(),
							null);
				}

				codeEditText.setText(null);
				accountEditText.setText(null);
				account.setCode(codeEditText.getText().toString());
				account.setAccount(accountEditText.getText().toString());
				account.setId(0L);

				showList("", "");
			}
		});

		Button searchButton = (Button) findViewById(R.id.searchAccountButton);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				account.setCode(codeEditText.getText().toString());
				account.setAccount(accountEditText.getText().toString());

				String code = account.getCode();
				String acc = account.getAccount();

				codeEditText.setText("");
				accountEditText.setText("");
				account.setCode(codeEditText.getText().toString());
				account.setAccount(accountEditText.getText().toString());
				account.setId(0L);

				showList(code, acc);
			}
		});
	}

	private void showList(String code, String account) {
		Intent intent = new Intent(ManageAccountActivity.this,
				MpfActivity.class);
		intent.putExtra(Account.CODE, code);
		intent.putExtra(Account.ACCOUNT, account);
		startActivityForResult(intent, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.manage_account, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent = IntentHandler.handleOnOptionsItemSelected(this,
				item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}
}
